﻿

function openDialogSelectPtoVta() {

    $("#dialog-modal-ptoVta").dialog("open");
    //$("#dialog-modal-ptoVta").parent().appendTo($("form:first"));
};

function fillCboPtoVta() {

    var Proxy = new serviceProxy("/Svc/ClientSearch.svc/");
    var empresaIdPar = parseInt($('#cboCompany').val());

    if (empresaIdPar != '') {
        var cboPtoVta = $('#cboPtoVta');

        cboPtoVta.attr('disabled', 'disabled');

        Proxy.invoke("GetPuntosDeVenta", { empresaId: empresaIdPar },
             function (result) {

                 cboPtoVta.html('');
                 var hidPtoVta = $('#hidPtoVta');
                 var hidPtoVtaIntValue = -1;
                 if (hidPtoVta.val() != '') {
                     hidPtoVtaIntValue = parseInt(hidPtoVta.val());
                 }

                 $.each(result, function () {

                     if (this.Id == hidPtoVtaIntValue) {
                         cboPtoVta.append($("<option selected='selected' />").val(this.Id).text(this.Nombre));
                     }
                     else {
                         cboPtoVta.append($("<option />").val(this.Id).text(this.Nombre));
                     }
                 });

             }, function (err) {
                 var errorPanel = new ErrorPanel();
                 errorPanel.showError('Error', 'Ocurrió un error al cargar los puntos de venta. Por favor reintente la operación');
             });

        cboPtoVta.removeAttr('disabled');
    }
};

function MarcarSeleccionDePtoVta() {

    var cboCompany = $('#cboCompany');
    var cboPtoVta = $('#cboPtoVta');
    var lblPtoVtaInfo = $('#lblPtoVtaInfo');
    var imgCompany = $('#ImgEmpresa');

    new ErrorPanel().removeAsteriskToJQControl(lblPtoVtaInfo);

    if (cboCompany.val() != '-1' && cboPtoVta.val() != '-1') {
        lblPtoVtaInfo.html('' + $('#cboPtoVta option:selected').text());

        if (companies) {
            //imgCompany.attr('src', companies[cboCompany.val()]);

            var companyCustomSplit = companies[cboCompany.val()].split('##');

            // Primero elimino todos los posibles            
            $("link[href^='/Styles/Company']").attr('disabled', true);
            $("link[href^='/Styles/Company']").remove();

            // Luego cambio el CSS y la imagen        
            imgCompany.attr('src', companyCustomSplit[0]);
            var urlStyleSheet = '/Styles/Company/' + companyCustomSplit[1];
            if (document.createStyleSheet) {
                document.createStyleSheet(urlStyleSheet);
            }
            else {
                $('head').append('<link href="' + urlStyleSheet + '" rel="stylesheet" type="text/css" />');
            }    

        }

    }
    else {
        lblPtoVtaInfo.html("Punto de Venta no seleccionado");
        imgCompany.attr('src', '');
    }
};


function ValidarSubmitLogin() {

    var txtUserName = $('#txtUserName');
    var txtPassword = $('#txtPassword');
    var cboCompany = $('#cboCompany');
    var cboPtoVta = $('#cboPtoVta');

    var lblPtoVtaInfo = $('#lblPtoVtaInfo');

    var errorPanel = new ErrorPanel();

    var isError = false;
    var strError = '';

    $('.errorAsterisk').remove();

    var separador = '';
    if (txtUserName.val() == '') {
        isError = true;
        errorPanel.appendAsteriskToJQControl(txtUserName);

        strError += separador;
        strError += 'Complete el nombre de usuario';
        separador = '<br />';
    }

    if (txtPassword.val() == '') {
        isError = true;
        errorPanel.appendAsteriskToJQControl(txtPassword);

        strError += separador;
        strError += 'Complete el password';
        separador = '<br />';
    }

    var isErrorPtoVta = false;
    if (cboCompany.val() == '-1') {
        isError = true;
        isErrorPtoVta = true;
    }

    if (cboPtoVta.val() == '-1') {
        isError = true;
        isErrorPtoVta = true;
    }

    if (isErrorPtoVta) {
        errorPanel.appendAsteriskToJQControl(lblPtoVtaInfo);
        strError += separador;
        strError += 'Seleccione el punto de venta';
        separador = '<br />';
    }

    if (isError) {
        errorPanel.showError('Por favor complete el formulario', strError);
    }

    return !isError;

};